package org.vacoor.ticket.handler.impl.option;

import java.io.UnsupportedEncodingException;

import org.vacoor.ticket.Constant;
import org.vacoor.ticket.bean.option.QueueInfo;
import org.vacoor.ticket.handler.ResultHandler;
import org.vacoor.ticket.util.ParseUtil;

public class QueueResultHandler implements ResultHandler<QueueInfo> {

	@Override
	public QueueInfo handler(byte[] content) {
		String result;
		try {
			result = new String(content,Constant.ENCODING);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			result = new String(content);
		}
//		System.out.println(new String(content));
		return parseQueueInfo(result);
	}

	/*
	 {
		"ticket":"1*****33184*****00401*****02353*****0001", //余票信息
		"countT":0,  //前面人数,这个是超过系统参数???
		"op_2":false //排队人数已经超过余票张数,不能提交
		"op_1":true, //与op_2一样,这个是之前版本使用
		"count":3,   //前面人数????
	 }
	 
	//https://dynamic.12306.cn/otsweb/js/order/save_passenger_info.js?version=5.42
	//function showOrderDialog(tFlag)
	*/
	
	public static QueueInfo parseQueueInfo(String json) {
		System.out.println(json);
		QueueInfo queue = new QueueInfo();
		//解析座位余票信息
		String seatTickets = ParseUtil.getValueFromJsonStr("ticket", json);
		queue.setAllSeatInfo(ParseUtil.parseTicketStr(seatTickets));
		
		String error = ParseUtil.getValueFromJsonStr("op_2", json);
		if("true".equalsIgnoreCase(error)) {
			queue.setBuy(false);
//			System.out.println("排队人数超过余票张数,无法预订");
		}else {
			queue.setBuy(true);
			String queueCount = ParseUtil.getValueFromJsonStr("countT", json);
			queue.setQueueCount(queueCount);
		}
		return queue;
	}
}
